/**
 * 
 */
package com.kingdee.eas.EDCommonUtil;

import java.math.BigDecimal;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.BOSObjectFactory;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.query.IQueryExecutor;
import com.kingdee.bos.dao.query.QueryExecutorFactory;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.framework.IObjectBase;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.PropertyContainer;

/**
 * @author dev.zhang
 * 
 */
public class EDCommonDataBeanHelper {

	public static IRowSet getRowSet(String queryName, FilterInfo fi) throws Exception {
		MetaDataPK queryPK = new MetaDataPK(queryName);
		IQueryExecutor exec = QueryExecutorFactory.getRemoteInstance(queryPK);
		exec.option().isAutoIgnoreZero = false;
		exec.option().isAutoTranslateBoolean = true;
		exec.option().isAutoTranslateEnum = true;
		EntityViewInfo evi = new EntityViewInfo();
		evi.setFilter(fi);
		exec.setObjectView(evi);
		IRowSet rowSet = exec.executeQuery();
		return rowSet;
	}
	
	public static boolean isEqules(BigDecimal sourceAmount, BigDecimal indentAmount) {
		if (sourceAmount != null && indentAmount != null) {
			if (sourceAmount.multiply(EDSystemCommonConstants.NumericalConstants.Hundred_Amount).subtract(
					indentAmount.multiply(EDSystemCommonConstants.NumericalConstants.Hundred_Amount)).abs().intValue() == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	public static IRowSet getRowSet(String queryName, EntityViewInfo viewInfo) throws Exception {
		MetaDataPK queryPK = new MetaDataPK(queryName);
		IQueryExecutor exec = QueryExecutorFactory.getRemoteInstance(queryPK);
		exec.option().isAutoIgnoreZero = false;
		exec.option().isAutoTranslateBoolean = true;
		exec.option().isAutoTranslateEnum = true;
		exec.setObjectView(viewInfo);
		IRowSet rowSet = exec.executeQuery();
		return rowSet;
	}

	public static int getRowCount(String queryName, EntityViewInfo viewInfo) throws Exception {
		MetaDataPK queryPK = new MetaDataPK(queryName, queryName);
		IQueryExecutor exec = QueryExecutorFactory.getRemoteInstance(queryPK);
		exec.option().isAutoIgnoreZero = false;
		exec.option().isAutoTranslateBoolean = true;
		exec.option().isAutoTranslateEnum = true;
		exec.setObjectView(viewInfo);
		return exec.getRowCount();
	}

	/**
	 * @param iObjectBase
	 * @param pk
	 * @return
	 * @throws EASBizException
	 * @throws BOSException
	 */
	public static CoreBaseInfo getObjectByParameter(IObjectBase iObjectBase, IObjectPK pk) throws EASBizException, BOSException {
		return iObjectBase.getValue(pk);
	}

	/**
	 * @param iObjectBase
	 * @param pk
	 * @return
	 * @throws EASBizException
	 * @throws BOSException
	 */
	public static PropertyContainer getPropertyContainerByParameter(ICoreBase iObjectBase, IObjectPK pk) throws EASBizException, BOSException {
		return iObjectBase.getValue(pk);
	}

	/**
	 * 
	 * 
	 * @param bosObjectType
	 *            entityValueInfo.bosObjectType.
	 * @param iObjectBaseClazz
	 *            entityValue.ibjectBase
	 * @param pk
	 * @return
	 * @throws BOSException
	 * @throws EASBizException
	 */
	public static CoreBaseInfo getCoreBaseByParameter(BOSObjectType bosObjectType, Class iObjectBaseClazz, IObjectPK pk) throws BOSException, EASBizException {
		IObjectBase objectBase = (IObjectBase) BOSObjectFactory.createRemoteBOSObject(bosObjectType, iObjectBaseClazz);
		return getObjectByParameter(objectBase, pk);
	}

	public static PropertyContainer getPropertyContainerByParameter(BOSObjectType bosObjectType, Class iObjectBaseClazz, IObjectPK pk) throws BOSException,
			EASBizException {
		ICoreBase coreBase = (ICoreBase) BOSObjectFactory.createRemoteBOSObject(bosObjectType, iObjectBaseClazz);
		return getPropertyContainerByParameter(coreBase, pk);
	}
	
	

}
